package cz.proks.easyhelpdesk.gwt.utils;

import java.util.HashMap;

import org.spiffyui.client.rest.RESTCallback;
import org.spiffyui.client.rest.RESTException;
import org.spiffyui.client.rest.RESTOptions;
import org.spiffyui.client.rest.RESTility;
import org.spiffyui.client.rest.RESTility.HTTPMethod;

import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;

public class RestUtils {
    
    public static void makeRestRequest(final String url, final RESTCallback callback) {
        makeRestRequest(url, null, RESTility.GET, callback);
    }
    
    public static void makeRestRequest(final String url, final JSONValue data, final RESTCallback callback) {
        makeRestRequest(url, data, RESTility.POST, callback);
    }
    
    public static void makeRestRequest(final String url, final JSONValue data, final HTTPMethod method, final RESTCallback callback) {
        RESTOptions opts = new RESTOptions();
        opts.setURL(url);        
        opts.setMethod(method);
        opts.setData(data);
        opts.setCallback(callback);
        
        RESTility.callREST(opts);
    }
    
    public static void makeSignedRestRequest(final String url, final String accessToken, final RESTCallback callback) {
        makeSignedRestRequest(url, accessToken, null, RESTility.GET, callback);
    }
    
    public static void makeSignedRestRequest(final String url, final String accessToken, final JSONValue data, final RESTCallback callback) {
        makeSignedRestRequest(url, accessToken, data, RESTility.POST, callback);
    }
    
    public static void makeSignedRestRequest(final String url, final String accessToken, final JSONValue data, final HTTPMethod method, final RESTCallback callback) {                       
        RESTOptions opts = new RESTOptions();
        opts.setURL(url);
        opts.setHeaders(getAuthorizedHeader(accessToken));
        opts.setMethod(method);
        opts.setData(data);
        opts.setCallback(callback);
                
        RESTility.callREST(opts);
    }        
    
    public static HashMap<String, String> getAuthorizedHeader(String accessToken) {
        return getAuthorizedHeader(null, accessToken);
    }
    
    public static HashMap<String, String> getAuthorizedHeader(HashMap<String, String> headers, String accessToken) {
        if (headers == null)
            headers = new HashMap<String, String>();
        
        headers.put("Authorization", "OAuth " + accessToken);
        return headers;        
    }
    
    public abstract static class Callback implements RESTCallback {                
        private RESTCallback delegateCallback;
        
        public Callback() {         
        }
        
        public Callback(RESTCallback delegateCallback) {
            this();
            this.delegateCallback = delegateCallback;
        }
                        
        @Override
        public void onError(int statusCode, String errorResponse) {
            if (delegateCallback != null)
                delegateCallback.onError(statusCode, errorResponse);
            
            else
                Window.alert("Status code " + statusCode + ", errorResponse " + errorResponse);
        }

        @Override
        public void onError(RESTException e) {
            if (delegateCallback != null)
                delegateCallback.onError(e);
            
            else {
                e.printStackTrace();                        
                Window.alert("REST call failed with exception described as " + e.getMessage());
            }
        }        
    }
}
